package com.example.librarymanagementproject2.web.controller;


import com.example.librarymanagementproject2.Common.config.ResultVO;
import com.example.librarymanagementproject2.Common.exception.GlobalException;
import com.example.librarymanagementproject2.Common.utils.MinioUtil;
import com.example.librarymanagementproject2.model.document.BorrowRecordDocument;
import com.example.librarymanagementproject2.model.document.UserDocument;
import com.example.librarymanagementproject2.model.entity.BorrowRecord;
import com.example.librarymanagementproject2.model.entity.User;
import com.example.librarymanagementproject2.service.UserService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @ClassName UserController
 * @Description TODO
 * @Author Kx
 * @Date 2025/9/28 20:41
 * @Version 1.0
 */
@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private MinioUtil minioUtil;

    /**
     * @author: kx
     * @date: 2025/10/28 9:50
     * @param pageNum : 页码
     * @param pageSize : 每页数量
     * @description: 分页查询所有用户信息
    */
    @GetMapping("/users/{pageNum}/{pageSize}")
    public ResultVO<List<UserDocument>> getAllUsers(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize) {
        return ResultVO.success(userService.getAllUsers(pageNum, pageSize));
    }

    /**
     * @author: kx
     * @date: 2025/10/28 9:45
     * @param key : 搜索关键字
     * @param pageNum : 页码
     * @param pageSize : 每页数量
     * @return com.example.librarymanagementproject2.Common.config.ResultVO<java.util.List<com.example.librarymanagementproject2.model.document.UserDocument>>
     * @description: 根据关键字分页查询用户信息
    */
    @GetMapping("/usersById/{key}/{pageNum}/{pageSize}")
    public ResultVO<List<UserDocument>> getUserById(@PathVariable("key") String key, @PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize) {
        return ResultVO.success(userService.getUserById(key, pageNum, pageSize));
    }

    /**
     * @author: kx
     * @date: 2025/10/14 19:26
     * @param request : 请求对象
     * @return org.example.common.config.ResultVO<java.util.List<org.example.model.entity.BorrowRecordDocument>>
     * @description: 根据当前登录用户ID查询该用户的借阅记录
    */
    @GetMapping("/users/borrowRecords")
    public ResultVO<List<BorrowRecordDocument>> getBorrowRecordsByUserId(HttpServletRequest request) {
        // 获取用户ID
        Long userId = (Long) request.getAttribute("userId");
        return ResultVO.success(userService.getBorrowRecordsByUserId(userId));
    }

    /**
     * @author: kx
     * @date: 2025/10/14 20:47
     * @param request : 请求对象
     * @param oldPassword : 旧密码
     * @param newPassword : 新密码
     * @return org.example.common.config.ResultVO<java.lang.String>
     * @description: 修改密码
    */
    @PostMapping("/users/password")
    public ResultVO<String> updatePassword(@RequestParam("oldPassword") String oldPassword,
                                           @RequestParam("newPassword") String newPassword,
                                           HttpServletRequest request) {
        // 获取用户ID
        Long userId = (Long) request.getAttribute("userId");
        userService.updatePassword(userId, oldPassword, newPassword);
        return ResultVO.success("修改密码成功");
    }

    /**
     * @author: kx
     * @date: 2025/10/27 8:44
     * @param image : 头像图片
     * @param request : 请求对象
     * @return com.example.librarymanagementproject2.Common.config.ResultVO<java.lang.String>
     * @description: 修改用户头像
    */
    @PostMapping("/users/avatar")
    public ResultVO<String> updateAvatar(@RequestPart("image") MultipartFile image, HttpServletRequest request) {
        // 获取用户ID
        Long userId = (Long) request.getAttribute("userId");

        // 上传图片
        String fileName = minioUtil.uploadImage(image);
        // 修改用户头像
        userService.updateAvatar(userId, fileName);
        return ResultVO.success("修改用户头像成功");
    }

    /**
     * @author: kx
     * @date: 2025/10/27 10:04
     * @param request : 请求对象
     * @return com.example.librarymanagementproject2.Common.config.ResultVO<java.lang.String>
     * @description: 注销账号
    */
    @PostMapping("/users/cancel")
    public ResultVO<String> cancelAccount(HttpServletRequest  request) {
        // 获取用户ID
        Long userId = (Long) request.getAttribute("userId");
        userService.cancelAccount(userId);
        return ResultVO.success("注销账号成功");
    }
}
